package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.discovery.activity.EditorArticleListActivity;
import com.rongshu.view.discovery.activity.SerialActivity;
import com.rongshu.view.home.bean.RecommendEditResponse;
import com.rongshu.view.mediaPlayer.activity.AudiobookListActivity;
import com.rongshu.widget.BanyanDialog;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 */
public class RecommendEditorAdapter extends RecyclerView.Adapter<RecommendEditorAdapter.ViewHolder> {
    private Context context;
    private List<RecommendEditResponse.ResponseBean> list;

    public RecommendEditorAdapter(Context context, List<RecommendEditResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_recommend_editor, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.editorName.setText(list.get(i).getTitle());
        GlideUtils.loadImageView(context, viewHolder.editorImg, list.get(i).getUrl(),
                TextUtils.equals(list.get(i).getTitle(), "连载") ? R.drawable.default_serialize_preview : R.drawable.default_novel_preview
                , TextUtils.equals(list.get(i).getTitle(), "连载") ? R.drawable.default_serialize_preview : R.drawable.default_novel_preview);
        viewHolder.editorImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.get(i).getIsLooking() == 0) {
                    BanyanDialog.Builder builder = new BanyanDialog.Builder(context);
                    builder.setTitle("更多精彩内容，敬请期待");
                    builder.setRight("我知道了");
                    final BanyanDialog dialog = builder.create();
                    builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
                        @Override
                        public void leftClickListener() {

                        }

                        @Override
                        public void rightClickListener() {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                } else {
                    if (TextUtils.equals(list.get(i).getTitle(), "连载")) {
                        Intent intent = new Intent(context, SerialActivity.class);
                        context.startActivity(intent);
                    } else if (TextUtils.equals(list.get(i).getTitle(), "声音")) {
                        Intent intent = new Intent(context, AudiobookListActivity.class);
                        intent.putExtra("articleBean", list.get(i));
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, EditorArticleListActivity.class);
                        intent.putExtra("articleBean", list.get(i));
                        context.startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView editorName;
        private ImageView editorImg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            editorImg = itemView.findViewById(R.id.editor_img);
            editorName = itemView.findViewById(R.id.editor_name);
        }
    }
}
